/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package umlapp.swingui.classdiagram;

import java.util.ArrayList;
import javax.swing.DefaultCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import umlapp.elements.InterfaceElement;
import umlapp.elements.members.Attribute;
import umlapp.elements.members.Operation;
import umlapp.elements.members.Visibility;
import umlapp.exception.UMLAppException;

/**
 *
 * Dialógove okno pre úpravu elementu typu Interface.
 *
 * @author Peter Chovanec
 * @author Mario Jaros
 */
public class InterfaceMembersDialog extends javax.swing.JDialog {

    /**
     * Element ktorý sa má upraviť.
     */
    private InterfaceElement element;
    /**
     * Pomocná premenná ozačujúca ktorá záložka je označená.
     */
    private int tabIndex;

    /**
     * Vytvorenie nového dialógu
     */
    public InterfaceMembersDialog(java.awt.Frame parent, boolean modal, InterfaceElement element) {
        super(parent, modal);
        this.element = element;
        initComponents();
        jTextField1.setText(element.getName());
        JComboBox comboBox = new JComboBox();
        comboBox.setModel(createComboBoxModel());
        jTableAttributes.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(comboBox));
        jTableOperations.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(comboBox));
        setLocationRelativeTo(this);
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT
     * modify this code. The content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableAttributes = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTableOperations = new javax.swing.JTable();
        jButtonOk = new javax.swing.JButton();
        jButtonAdd = new javax.swing.JButton();
        jButtonRemove = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jTabbedPane1.setName("Attributes"); // NOI18N
        jTabbedPane1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jTabbedPane1StateChanged(evt);
            }
        });

        jTextField1.setText("jTextField1");

        jLabel1.setFont(new java.awt.Font("Gill Sans MT", 0, 14)); // NOI18N
        jLabel1.setText("Name:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(203, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(193, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("General", jPanel1);

        jTableAttributes.setModel(createAttributesTableModel());
        jTableAttributes.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(jTableAttributes);

        jTabbedPane1.addTab("Attributes", jScrollPane1);

        jTableOperations.setModel(createOperationsTableModel());
        jScrollPane2.setViewportView(jTableOperations);

        jTabbedPane1.addTab("Operations", jScrollPane2);

        jButtonOk.setText("Ok");
        jButtonOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOkActionPerformed(evt);
            }
        });

        jButtonAdd.setText("Add Line");
        jButtonAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAddActionPerformed(evt);
            }
        });

        jButtonRemove.setText("Remove Line");
        jButtonRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRemoveActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButtonRemove)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonAdd)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonOk)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 252, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonOk)
                    .addComponent(jButtonAdd)
                    .addComponent(jButtonRemove))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTabbedPane1.getAccessibleContext().setAccessibleName("attributes");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * pomocná metóda na vytvorenie modelu tabuľky atribútov
     *
     * @return
     */
    private javax.swing.table.DefaultTableModel createAttributesTableModel() {
        ArrayList<Attribute> attributes = element.getAttributes();
        Object[][] objects = new Object[attributes.size()][3];
        for (int i = 0; i < attributes.size(); i++) {
            objects[i][0] = attributes.get(i).getName();
            objects[i][1] = attributes.get(i).getDataType();
            objects[i][2] = attributes.get(i).getVisibility();
        }
        return new javax.swing.table.DefaultTableModel(
                objects,
                new String[]{
                    "Name", "Data Type", "Visibility"
                }) {
            Class[] types = new Class[]{
                String.class, String.class, Visibility.class
            };

            @Override
            public Class getColumnClass(int columnIndex) {
                return types[columnIndex];
            }
        };
    }

    /**
     * pomocná metóda na vytvorenie modelu tabuľky operácii
     *
     * @return
     */
    private javax.swing.table.DefaultTableModel createOperationsTableModel() {
        ArrayList<Operation> operations = element.getOperations();
        Object[][] objects = new Object[operations.size()][3];
        for (int i = 0; i < operations.size(); i++) {
            objects[i][0] = operations.get(i).getName();
            objects[i][1] = operations.get(i).getReturnDataType();
            objects[i][2] = operations.get(i).getVisibility();
        }
        return new javax.swing.table.DefaultTableModel(
                objects,
                new String[]{
                    "Name", "Return Type", "Visibility"
                }) {
            Class[] types = new Class[]{
                String.class, String.class, Visibility.class
            };

            @Override
            public Class getColumnClass(int columnIndex) {
                return types[columnIndex];
            }
        };
    }

    /**
     * Pomocná metóda na vytvorenie ComboBoxu
     *
     * @return
     */
    private DefaultComboBoxModel createComboBoxModel() {
        return new javax.swing.DefaultComboBoxModel(new Visibility[]{Visibility.PUBLIC, Visibility.PROTECTED, Visibility.PRIVATE, Visibility.PACKAGE});
    }

    /**
     * Implementácia tlačidla OK Náčita údaje z tabuliek a priradí ich práve upravevoného elementu
     *
     * @param evt
     */
    private void jButtonOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOkActionPerformed

        ArrayList<Attribute> attributes = new ArrayList<>();
        ArrayList<Operation> operations = new ArrayList<>();

        for (int i = 0; i < jTableAttributes.getRowCount(); i++) {
            try {
                attributes.add(new Attribute(
                        (String) jTableAttributes.getValueAt(i, 0),
                        (String) jTableAttributes.getValueAt(i, 1),
                        (Visibility) jTableAttributes.getValueAt(i, 2)));
            } catch (UMLAppException ex) {
                continue;
            }

        }

        for (int i = 0; i < jTableOperations.getRowCount(); i++) {
            try {
                operations.add(new Operation(
                        (String) jTableOperations.getValueAt(i, 0),
                        (String) jTableOperations.getValueAt(i, 1),
                        (Visibility) jTableOperations.getValueAt(i, 2)));
            } catch (UMLAppException ex) {
                continue;
            }

        }
        element.setAttributes(attributes);
        element.setOperations(operations);
        element.setName(jTextField1.getText());
        this.setVisible(false);
        this.dispose();
        super.getParent().repaint();
    }//GEN-LAST:event_jButtonOkActionPerformed
    /**
     * Implementácia tlačidla Add Vytvorý ďalši riadok v tabuľke
     *
     * @param evt
     */
    private void jButtonAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddActionPerformed
        Object[] o = new Object[]{null, null, null};
        if (tabIndex == 1) {
            ((javax.swing.table.DefaultTableModel) jTableAttributes.getModel()).addRow(o);
        } else if (tabIndex == 2) {
            ((javax.swing.table.DefaultTableModel) jTableOperations.getModel()).addRow(o);
        }
    }//GEN-LAST:event_jButtonAddActionPerformed
    /**
     * Implementácia tlačidla Remove Zruši riadok v tabuľke
     *
     * @param evt
     */
    private void jButtonRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRemoveActionPerformed
        if (tabIndex == 1) {
            int index = jTableAttributes.getSelectedRow();
            ((javax.swing.table.DefaultTableModel) jTableAttributes.getModel()).removeRow(index);
        } else if (tabIndex == 2) {
            int index = jTableOperations.getSelectedRow();
            ((javax.swing.table.DefaultTableModel) jTableOperations.getModel()).removeRow(index);
        }
    }//GEN-LAST:event_jButtonRemoveActionPerformed
    /**
     * Pomocná metóda na určovanie tab
     *
     * @param evt
     */
    private void jTabbedPane1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jTabbedPane1StateChanged
        // TODO add your handling code here:
        tabIndex = jTabbedPane1.getSelectedIndex();
    }//GEN-LAST:event_jTabbedPane1StateChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAdd;
    private javax.swing.JButton jButtonOk;
    private javax.swing.JButton jButtonRemove;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable jTableAttributes;
    private javax.swing.JTable jTableOperations;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
